<template>
  <view class="container">
    <!-- 背景动画 -->
    <view class="bg-animation">
      <view class="circle circle-1"></view>
      <view class="circle circle-2"></view>
      <view class="circle circle-3"></view>
      <view class="circle circle-4"></view>
      <view class="circle circle-5"></view>
    </view>

    <!-- 主内容区域 -->
    <scroll-view class="main-content" scroll-y @scroll="handleScroll" :scroll-top="scrollTop">
      <!-- 登录卡片 -->
      <view class="login-card" :style="{ 
        transform: `translateY(${cardOffset}px) scale(${cardScale})`,
        opacity: cardOpacity
      }">
        <view class="logo-area">
          <image class="logo" src="/static/logo.png" mode="aspectFit"></image>
          <text class="company-name">村村通配送员端</text>
          <text class="slogan">连接乡村，传递温暖</text>
        </view>

        <view class="form-container">
          <view class="form-item" :class="{ 'input-active': isPhoneFocused }">
            <uni-icons type="phone" size="22" color="#7a8cb1"></uni-icons>
            <input class="input" type="number" v-model="phone" placeholder="请输入配送员手机号" maxlength="11"
              @focus="isPhoneFocused = true" @blur="isPhoneFocused = false" />
          </view>

          <view class="form-item" :class="{ 'input-active': isPwdFocused }">
            <uni-icons type="locked" size="22" color="#7a8cb1"></uni-icons>
            <input class="input" :type="showPassword ? 'text' : 'password'" v-model="password"
              placeholder="请输入密码" @focus="isPwdFocused = true" @blur="isPwdFocused = false" />
            <uni-icons :type="showPassword ? 'eye' : 'eye-slash'" size="22" color="#7a8cb1"
              @click="togglePasswordVisibility"></uni-icons>
          </view>

          <button class="login-btn" @click="handleLogin" :class="{ 'btn-active': phone && password }"
            hover-class="btn-hover">
            <text v-if="!isLogging">登 录</text>
            <view v-else class="loading">
              <view class="dot dot-1"></view>
              <view class="dot dot-2"></view>
              <view class="dot dot-3"></view>
            </view>
          </button>

          <view class="extra-links">
            <text @click="navTo('/pages/courier/register')">注册配送员</text>
            <text @click="navTo('/pages/courier/forget')">忘记密码</text>
          </view>
        </view>
      </view>

      <!-- 功能介绍部分 -->
      <view class="intro-section">
        <!-- 装饰元素 -->
        <view class="decor-element decor-1"></view>
        <view class="decor-element decor-2"></view>

        <view class="section-title">
          <text class="title-text">配送员专属功能</text>
          <view class="title-line"></view>
        </view>

        <view class="intro-card" v-for="(item, index) in introItems" :key="index"
          :style="{ animationDelay: `${index * 0.1}s` }">
          <view class="card-header">
            <uni-icons :type="item.iconType" size="24" color="#4a6baf"></uni-icons>
            <text class="card-title">{{ item.title }}</text>
          </view>
          <text class="card-content">{{ item.content }}</text>
        </view>

        <!-- 工作流程 -->
        <view class="section-title">
          <text class="title-text">工作流程</text>
          <view class="title-line"></view>
        </view>

        <view class="process-steps">
          <view class="step-item" v-for="(step, index) in processSteps" :key="'step'+index"
            :style="{ animationDelay: `${index * 0.2}s` }">
            <view class="step-number">{{ index + 1 }}</view>
            <text class="step-title">{{ step.title }}</text>
            <text class="step-desc">{{ step.desc }}</text>
          </view>
        </view>

        <!-- 收入说明 -->
        <view class="income-section">
          <text class="income-title">收入透明，多劳多得</text>
          <text class="income-desc">每单收入 = 基础配送费 + 距离补贴 + 好评奖励</text>
          <view class="income-example">
            <text>示例：</text>
            <text>基础配送费 ¥5 + 距离补贴 ¥3 + 好评奖励 ¥2 = ¥10/单</text>
          </view>
        </view>
      </view>
    </scroll-view>

    <!-- 返回顶部按钮 -->
    <view class="back-top" v-if="showBackTop" @click="scrollToTop" :style="{ opacity: backTopOpacity }">
      <uni-icons type="top"></uni-icons>
    </view>

    <!-- 浮动小元素 -->
    <view class="floating-elements">
      <view class="float-element float-1"></view>
      <view class="float-element float-2"></view>
      <view class="float-element float-3"></view>
    </view>
  </view>
</template>
<script>
import {
  globalVariable
} from '../../configMy.js';
export default {
  data() {
    return {
      phone: '',
      password: '',
      showPassword: false,
      isPhoneFocused: false,
      isPwdFocused: false,
      isLogging: false,
      scrollTop: 0,
      scrollPosition: 0,
      cardOffset: 0,
      cardScale: 1,
      cardOpacity: 1,
      showBackTop: false,
      backTopOpacity: 0,
      introItems: [{
          iconType: 'location',
          title: '智能派单',
          content: '系统智能分配附近订单，减少空跑，提高配送效率'
        },
        {
          iconType: 'map',
          title: '路线规划',
          content: '自动规划最优配送路线，避开拥堵路段，节省时间'
        },
        {
          iconType: 'wallet',
          title: '实时收益',
          content: '每单收益实时到账，随时提现，资金安全有保障'
        }
      ],
      processSteps: [{
          title: '接收订单',
          desc: '系统智能分配附近订单，接单后联系收件人'
        },
        {
          title: '取件确认',
          desc: '到达取件点，扫描快递单号确认取件'
        },
        {
          title: '配送途中',
          desc: '按照系统规划路线进行配送'
        },
        {
          title: '送达确认',
          desc: '到达目的地，收件人确认收货'
        },
        {
          title: '完成评价',
          desc: '收件人评价服务，获得好评奖励'
        }
      ]
    }
  },
  mounted() {
    this.startCardAnimation();
    this.loadCachedData();
  },
  methods: {
    loadCachedData() {
      const cachedUser = uni.getStorageSync('courierInfo');
      if (cachedUser) {
        this.phone = cachedUser.phone || '';
        this.password = cachedUser.password || '';
      }
    },
    togglePasswordVisibility() {
      this.showPassword = !this.showPassword;
    },
    startCardAnimation() {
      let scale = 0.95
      const animate = () => {
        scale = scale === 0.95 ? 0.95 : 0.95
        this.cardScale = scale
        setTimeout(animate, 1500)
      }
      animate()
    },
    async handleLogin() {
      // 表单验证
      if (!this.phone) {
        this.shakeForm()
        uni.showToast({
          title: '请输入手机号',
          icon: 'none'
        })
        return
      }

      if (!/^1[3-9]\d{9}$/.test(this.phone)) {
        this.shakeForm()
        uni.showToast({
          title: '手机号格式不正确',
          icon: 'none'
        })
        return
      }

      if (!this.password) {
        this.shakeForm()
        uni.showToast({
          title: '请输入密码',
          icon: 'none'
        })
        return
      }

      this.isLogging = true

      try {
        // 调用配送员登录接口
        const response = await uni.request({
          url: globalVariable + '/courier/login',
          method: 'POST',
          data: {
            phone: this.phone,
            password: this.password
          },
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          }
        })

        const result = response.data;

        if (result.status === 1) {
          this.playSuccessAnimation();
          
          // 存储配送员信息
          uni.setStorageSync('courierInfo', result.data);

          uni.showToast({
            title: '登录成功',
            icon: 'success'
          });

          setTimeout(() => {
            uni.switchTab({
              url: '/pages/index/index'
            });
          }, 1500);
        } else {
          this.shakeForm();
          uni.showToast({
            title: result.message || '登录失败',
            icon: 'none'
          });
        }
      } catch (err) {
        console.error('登录出错:', err);
        uni.showToast({
          title: '登录失败，请稍后再试',
          icon: 'none'
        });
      } finally {
        this.isLogging = false;
      }
    },
    shakeForm() {
      this.cardOffset = -5
      setTimeout(() => {
        this.cardOffset = 5
        setTimeout(() => {
          this.cardOffset = -5
          setTimeout(() => {
            this.cardOffset = 5
            setTimeout(() => {
              this.cardOffset = 0
            }, 50)
          }, 50)
        }, 50)
      }, 50)
    },
    playSuccessAnimation() {
      const originalScale = this.cardScale
      this.cardScale = originalScale * 0.9
      setTimeout(() => {
        this.cardScale = originalScale * 1.1
        setTimeout(() => {
          this.cardScale = originalScale
        }, 300)
      }, 300)
    },
    navTo(url) {
      uni.navigateTo({
        url: url
      })
    },
    handleScroll(e) {
      this.scrollPosition = e.detail.scrollTop

      if (this.scrollPosition < 300) {
        this.cardOffset = this.scrollPosition * 0.3
        this.cardOpacity = 1 - (this.scrollPosition / 600)
      }

      if (this.scrollPosition > 500) {
        this.showBackTop = true
        this.backTopOpacity = Math.min(1, (this.scrollPosition - 500) / 200)
      } else {
        this.showBackTop = false
      }
    },
    scrollToTop() {
      this.scrollTop = this.scrollTop === 0 ? 1 : 0
      setTimeout(() => {
        this.scrollTop = 0
      }, 10)
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  height: 100vh;
  background-image: radial-gradient(circle at 10% 20%, rgba(74, 144, 226, 0.1) 0, transparent 20%), 
                    radial-gradient(circle at 90% 80%, rgba(76, 175, 80, 0.1) 0, transparent 20%);
  overflow: hidden;
  position: relative;
}

.bg-animation {
  position: absolute;
  width: 100%;
  height: 100%;

  .circle {
    position: absolute;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.3);
    animation: float 15s infinite ease-in-out;
    filter: blur(1px);
    opacity: 0.8;
  }

  .circle-1 {
    width: 300px;
    height: 300px;
    top: 10%;
    left: 5%;
    background: rgba(74, 144, 226, 0.2);
    animation-delay: 0s;
    animation-duration: 20s;
  }

  .circle-2 {
    width: 250px;
    height: 250px;
    bottom: 15%;
    right: 5%;
    background: rgba(76, 175, 80, 0.2);
    animation-delay: 3s;
    animation-duration: 18s;
  }

  .circle-3 {
    width: 200px;
    height: 200px;
    top: 50%;
    left: 20%;
    background: rgba(255, 193, 7, 0.2);
    animation-delay: 6s;
    animation-duration: 16s;
  }

  .circle-4 {
    width: 180px;
    height: 180px;
    bottom: 30%;
    right: 20%;
    background: rgba(156, 39, 176, 0.2);
    animation-delay: 9s;
    animation-duration: 14s;
  }

  .circle-5 {
    width: 150px;
    height: 150px;
    top: 20%;
    right: 15%;
    background: rgba(33, 150, 243, 0.2);
    animation-delay: 12s;
    animation-duration: 12s;
  }
}

.main-content {
  width: 100%;
  height: 100%;
  position: relative;
  z-index: 10;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.login-card {
  height: 70vh;
  width: 85%;
  margin: 0 auto;
  background-color: rgba(255, 255, 255, 0.98);
  border-radius: 24px;
  padding: 30px;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
  transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.1);
  transform-origin: center;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-flow: column;
  margin-top: 100rpx;

  .logo-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 30px;

    .logo {
      width: 100px;
      height: 100px;
      margin-bottom: 15px;
      animation: pulse 2.5s infinite;
    }

    .company-name {
      font-size: 24px;
      font-weight: bold;
      color: #4a6baf;
      margin-bottom: 8px;
    }

    .slogan {
      font-size: 14px;
      color: #7a8cb1;
    }
  }
}

.form-container {
  width: 100%;

  .form-item {
    display: flex;
    align-items: center;
    background-color: #f8f9fa;
    border-radius: 50px;
    padding: 12px 22px;
    margin-bottom: 20px;
    transition: all 0.3s;
    border: 1px solid #e1e6eb;

    &.input-active {
      border-color: #4a6baf;
      box-shadow: 0 0 0 2px rgba(74, 107, 175, 0.2);
    }

    .input {
      flex: 1;
      height: 40px;
      font-size: 16px;
      color: #333;
      margin-left: 10px;
    }
  }
}

.login-btn {
  width: 100%;
  height: 52px;
  background: linear-gradient(to right, #4a6baf, #6a8ccc);
  color: white;
  border-radius: 50px;
  font-size: 18px;
  font-weight: bold;
  border: none;
  margin-top: 15px;
  transition: all 0.3s;
  box-shadow: 0 8px 20px rgba(74, 107, 175, 0.3);

  &.btn-active {
    background: linear-gradient(to right, #6a8ccc, #4a6baf);
  }

  &.btn-hover {
    transform: translateY(-2px);
    box-shadow: 0 12px 25px rgba(74, 107, 175, 0.4);
  }

  .loading {
    display: flex;
    justify-content: center;
    align-items: center;

    .dot {
      width: 8px;
      height: 8px;
      background: white;
      border-radius: 50%;
      margin: 0 4px;
      animation: bounce 1.5s infinite ease-in-out;
    }

    .dot-1 {
      animation-delay: 0s;
    }
    .dot-2 {
      animation-delay: 0.2s;
    }
    .dot-3 {
      animation-delay: 0.4s;
    }
  }
}

.extra-links {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  font-size: 14px;
  color: #7a8cb1;

  text {
    &:active {
      color: #4a6baf;
    }
  }
}

.intro-section {
  width: 85%;
  margin: 0 auto 40px;
  padding-top: 50px;
}

.section-title {
  margin-bottom: 30px;
  position: relative;

  .title-text {
    font-size: 20px;
    font-weight: bold;
    color: #4a6baf;
    position: relative;
    display: inline-block;
    padding: 0 15px;
    background: linear-gradient(135deg, #4a6baf, #6a8ccc);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
  }

  .title-line {
    position: absolute;
    bottom: 5px;
    left: 0;
    width: 100%;
    height: 1px;
    background: rgba(74, 107, 175, 0.2);

    &::before {
      content: '';
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100px;
      height: 3px;
      background: linear-gradient(to right, #4a6baf, #6a8ccc);
      border-radius: 3px;
    }
  }
}

.intro-card {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 16px;
  padding: 20px;
  margin-bottom: 20px;
  border: 1px solid rgba(74, 107, 175, 0.1);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
  animation: fadeInUp 0.5s both;

  .card-header {
    display: flex;
    align-items: center;
    margin-bottom: 15px;

    .card-title {
      font-size: 18px;
      font-weight: bold;
      color: #4a6baf;
      margin-left: 10px;
    }
  }

  .card-content {
    font-size: 14px;
    color: #666;
    line-height: 1.6;
  }
}

.process-steps {
  display: flex;
  flex-direction: column;
  gap: 20px;

  .step-item {
    background: rgba(255, 255, 255, 0.9);
    border-radius: 16px;
    padding: 20px;
    border: 1px solid rgba(74, 107, 175, 0.1);
    position: relative;
    animation: fadeInUp 0.5s both;

    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 4px;
      height: 100%;
      background: linear-gradient(to bottom, #4a6baf, #6a8ccc);
    }

    .step-number {
      position: absolute;
      top: 15px;
      right: 15px;
      width: 30px;
      height: 30px;
      background: linear-gradient(135deg, #4a6baf, #6a8ccc);
      color: white;
      border-radius: 50%;
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 14px;
      font-weight: bold;
    }

    .step-title {
      display: block;
      font-size: 16px;
      font-weight: bold;
      color: #4a6baf;
      margin-bottom: 8px;
    }

    .step-desc {
      font-size: 14px;
      color: #666;
      line-height: 1.5;
    }
  }
}

.income-section {
  background: rgba(255, 255, 255, 0.9);
  border-radius: 16px;
  padding: 25px;
  margin-top: 40px;
  text-align: center;
  border: 1px solid rgba(74, 107, 175, 0.1);
  animation: fadeInUp 0.5s both;

  .income-title {
    display: block;
    font-size: 18px;
    font-weight: bold;
    color: #4a6baf;
    margin-bottom: 10px;
  }

  .income-desc {
    display: block;
    font-size: 14px;
    color: #666;
    margin-bottom: 15px;
  }

  .income-example {
    font-size: 14px;
    color: #4a6baf;
    background: rgba(74, 107, 175, 0.1);
    padding: 10px;
    border-radius: 8px;
  }
}

.back-top {
  position: fixed;
  bottom: 30px;
  right: 20px;
  width: 50px;
  height: 50px;
  background: linear-gradient(135deg, #4a6baf, #6a8ccc);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  z-index: 100;
  transition: all 0.3s;

  &:active {
    transform: scale(0.9);
  }
}

/* 动画效果 */
@keyframes float {
  0%, 100% {
    transform: translate(0, 0) rotate(0deg);
  }
  25% {
    transform: translate(20px, -30px) rotate(5deg);
  }
  50% {
    transform: translate(-15px, 20px) rotate(-5deg);
  }
  75% {
    transform: translate(10px, -10px) rotate(3deg);
  }
}

@keyframes pulse {
  0%, 100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.05);
  }
}

@keyframes bounce {
  0%, 100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-8px);
  }
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>